﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TankBattle.Models
{
    public class Bullet: GameObject
    {
        #region Variable
        private int flag;
        private Sprite sprite;
        private int type;
        private int damage;
        int vx, vy;
        #endregion

        #region Contruction 

        public Bullet()
        { }
        public Bullet(Texture2D texture,Vector2 postion, int width, int height, int flag, int type)
        {
            this.flag = flag;
            this.type = type;
            Intallization(texture, postion, width, height);
        }
        public Bullet(Texture2D texture, Vector2 position,int flag, int type)
        {
            this.flag = flag;
            this.type = type;
            Intallization(texture, position, Constant.BULLET_WIDTH, Constant.BULLET_HEIGHT);
        }

        #endregion

        #region Properties
        public int Damage
        {
            get { return damage; }
        }
        public override Rectangle Polygon
        {
            get
            {
                return new Rectangle(
                    (int)position.X,
                    (int)position.Y,
                    width,
                    height);
            }
        }
        #endregion

        #region Function

        public override  bool Update()
        {
            if (sprite.Update()) SetState();
            // vì bullet chỉ đi theo một đường thẳng.
            position.X += vx * Constant.BULLET_STEP;
            position.Y += vy * Constant.BULLET_STEP;

            return base.Update();
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
            sprite.Draw(spriteBatch, position);
        }
        protected override void Intallization(Texture2D texture, Vector2 position, int width, int height)
        {
            base.Intallization(texture, position, width, height);

            sprite = new Sprite(texture, new Vector2(width, height),
                new Vector2(Constant.BULLET_SHEET_SIZE_X,
                    Constant.BULLET_SHEET_SIZE_Y), Constant.RATE_BULLET);
            SetState();
            SetMove();
            SetDamage();
        }
        protected virtual void SetDamage()
        {
            switch (type)
            {
                case (Constant.BULLET_TYPE_ROCKET):
                    damage = Constant.TANK_DAMAGE_HIGHT;
                    break;
                case (Constant.BULLET_TYPE_NORMAL):
                    damage = Constant.TANK_DAMAGE_NORMAL;
                    break;
            }
        }
        protected virtual void SetState()
        {
            switch (flag)
            {
                case (Constant.MOVE_DOWN):
                    sprite.SetCurrentFrame(new Vector2(0, Constant.FRAME_DOWN));
                    break;
                case (Constant.MOVE_UP):
                    sprite.SetCurrentFrame(new Vector2(0, Constant.FRAME_UP));
                    break;
                case (Constant.MOVE_LEFT):
                    sprite.SetCurrentFrame(new Vector2(0, Constant.FRAME_LEFT));
                    break;
                case (Constant.MOVE_RIGHT):
                    sprite.SetCurrentFrame(new Vector2(0, Constant.FRAME_RIGHT));
                    break;
            }
        }
        protected virtual void SetMove()
        {
            //flag là hướng di chuyển ban đầu cho bullet
            vx = vy = Constant.ZERO;
            switch (flag)
            {
                case Constant.MOVE_UP: vy = -Constant.V1_BULLET;
                    break;
                case Constant.MOVE_DOWN: vy = Constant.V1_BULLET;
                    break;
                case Constant.MOVE_LEFT: vx = -Constant.V1_BULLET;
                    break;
                case Constant.MOVE_RIGHT: vx = Constant.V1_BULLET;
                    break;
            }
        }
        public virtual Vector2 GetBoomSpritePosition()
        {
            Vector2 position = new Vector2(this.position.X, this.position.Y);

            switch (flag)
            {
                case (Constant.MOVE_DOWN):
                    position.X += -Constant.BOOM_SIZE / 2 + Constant.BULLET_WIDTH / 2;
                    position.Y += Constant.BULLET_HEIGHT;
                    break;
                case (Constant.MOVE_UP):
                    position.X += -Constant.BOOM_SIZE / 2 + Constant.BULLET_WIDTH / 2;
                    position.Y -= Constant.BOOM_SIZE;
                    break;
                case (Constant.MOVE_LEFT):
                    position.X -= Constant.BOOM_SIZE;
                    position.Y += Constant.BULLET_HEIGHT / 2 - Constant.BOOM_SIZE / 2;
                    break;
                case (Constant.MOVE_RIGHT):
                    position.X += Constant.BULLET_WIDTH;
                    position.Y += Constant.BULLET_HEIGHT / 2 - Constant.BOOM_SIZE / 2;
                    break;
            }
            return position;
        }
    
        #endregion
    }
}
